<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="200">
      <el-row :gutter="24">
        <el-col :md="6" :sm="12">
          <el-form-item label="车辆名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输车辆名称"
              clearable
              style="width: 140px;"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :md="6" :sm="12">
          <el-form-item label="车辆类型" prop="cartype">
            <el-select
              v-model="queryParams.cartype"
              placeholder="请输入门店地址"
              clearable
              @keyup.enter.native="handleQuery"
            >
              <el-option
                v-for="dict in dict.type.vehicle_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :md="6" :sm="12">
          <el-form-item label="请输入车牌号" prop="name">
            <el-input
              v-model="queryParams.carnum"
              placeholder="车牌号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :md="6" :sm="12">
          <el-form-item style="margin-bottom: 20px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <a style="margin-left: 8px" @click="toggleAdvanced">
              {{ advanced ? '收起' : '展开' }}
              <el-icon :class="advanced?'el-icon-arrow-up':'el-icon-arrow-down'"/>
            </a></el-form-item>
        </el-col>
        <el-collapse-transition>
          <div v-show="advanced">

            <el-col :md="6" :sm="12">
              <el-form-item label="车身结构" prop="status">
                <el-select
                  v-model="queryParams.structure"
                  placeholder="请选择车身结构"
                  clearable
                  @keyup.enter.native="handleQuery"
                  style="width: 130px;">
                  <el-option
                    v-for="dict in dict.type.vehicle_structure"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="变速箱" prop="status">
                <el-select
                  v-model="queryParams.grarbox"
                  placeholder="请选择变速箱类型"
                  @keyup.enter.native="handleQuery"
                  style="width: 130px;">
                  <el-option
                    v-for="dict in dict.type.vehicle_grarbox"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="租车服务筛选" prop="isRentalCar">
                <el-select
                  v-model="queryParams.isRental"
                  placeholder="请选择是否开启租车服务"
                  @keyup.enter.native="handleQuery"
                  style="width: 130px;">
                  <el-option
                    v-for="dict in dict.type.veh_chartered"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="包车服务筛选" prop="isCharterCar">
                <el-select
                  v-model="queryParams.isCharter"
                  placeholder="请选择是否开启包车服务"
                  @keyup.enter.native="handleQuery"
                  style="width: 130px;">
                  <el-option
                    v-for="dict in dict.type.veh_isrental"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="车辆状态" prop="isCharterCar">
                <el-select
                  v-model="queryParams.status"
                  placeholder="请选择车辆状态"
                  @keyup.enter.native="handleQuery"
                  style="width: 130px;">
                  <el-option
                    v-for="dict in dict.type.vehicle_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </div>
        </el-collapse-transition>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['merchant:goods:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['merchant:goods:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['merchant:goods:add']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleBingGPS"
          v-hasPermi="['merchant:goods:add']"
        >绑定GPS
        </el-button>
      </el-col>
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>
    <el-table v-loading="loading" lazy :data="storeList" row-key="id"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="车辆照片" align="center" prop="pic">
        <template slot-scope="scope">
          <img v-bind:src="scope.row.pic" class="img-circle-shop img-lg-shop"/>
        </template>
      </el-table-column>
      <el-table-column label="车辆id" align="center" prop="id"/>
      <el-table-column label="车辆名称" align="center" prop="name" width="200"/>
      <el-table-column label="车牌号" align="center" prop="carnum"/>
      <el-table-column label="变速箱" align="center">
        <template v-slot:default="scope">
          <dict-tag :options="dict.type.vehicle_grarbox" :value="scope.row.grarbox"></dict-tag>
<!--          {{ getLabel(scope.row.grarbox, 'vehicle_grarbox') }}-->
        </template>
      </el-table-column>
      <el-table-column label="车辆类型" align="center" prop="linkman">
        <template v-slot:default="scope">
          <dict-tag :options="dict.type.vehicle_type" :value="scope.row.cartype"></dict-tag>
<!--          {{ getLabel(scope.row.cartype, 'vehicle_type') }}-->
        </template>
      </el-table-column>
      <el-table-column label="颜色" align="center" prop="colour"/>
      <el-table-column label="车身结构" align="center" prop="shopTel">
        <template v-slot:default="scope">
          <dict-tag :options="dict.type.vehicle_structure" :value="scope.row.structure"></dict-tag>
<!--          {{ getLabel(scope.row.structure, 'vehicle_structure') }}-->
        </template>
      </el-table-column>
      <el-table-column label="车辆状态" align="center" prop="status">
        <template v-slot:default="scope">
          <dict-tag :options="dict.type.vehicle_status" :value="scope.row.status"></dict-tag>
<!--          {{ getLabel(scope.row.status, 'vehicle_status') }}-->
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" width="200">
        <template v-slot:default="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            size="mini"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['merchant:goods:edit']"
          >修改
          </el-button>
          <el-button
            type="text"
            icon="el-icon-map-location"
            size="mini"
            v-if="scope.row.imei&&scope.row.imei!=''"
            @click="handleMapLocation(scope.row)"
          >获取定位
          </el-button>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)"
                       v-hasPermi="['system:user:resetPwd', 'system:user:edit']">
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="handleDelete"
                icon="el-icon-delete"
                v-hasPermi="['merchant:goods:remove']"
              >删除
              </el-dropdown-item>
              <el-dropdown-item command="handleBingGPS" icon="el-icon-key"
                                v-hasPermi="['merchant:goods:add']">绑定GPS
              </el-dropdown-item>
              <!--                  <el-dropdown-item command="handleDismiss" icon="el-icon-key"-->
              <!--                                    v-hasPermi="['person:staff:dismiss']">解雇-->
              <!--                  </el-dropdown-item>-->
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <add-vehicle @update="getList" ref="addVehicle" :status-options="statusOptions"
                 :shop-ranges-option="shopRangesOption"/>
  </div>
</template>
<script>
import addVehicle from './computed/addVehicle.vue'
import {listData} from "@/api/system/dict/data";
import {delCar, getCar, listCar} from "@/api/merchant/car";

export default {
  name: "index",
  dicts: ['vehicle_status', 'vehicle_structure', 'vehicle_type', 'vehicle_grarbox', 'veh_isrental', 'veh_chartered'],
  components: {addVehicle},
  data() {
    return {
      total: 0,
      ids: [],
      single: false,
      multiple: true,
      statusOptions: [{
        value: '营业',
        label: 1
      }, {
        value: '关闭',
        label: 0
      }],
      shopRangesOption: [],
      loading: true,
      storeList: [],
      provinceOption: [],
      cityOption: [],
      areaOption: [],
      // 显示搜索
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isRental: '',
        isCharter: '',
        status: '',        //车辆状态
        sid: '',          //门店id
        cartype: '',         //门店名称
        carnum: '',      //车牌号
        structure: '',    //车身结果
        grarbox: '',        //所在城市
        displacement: '',        //所在区域
        name: '',           // 车辆名称
      },
      advanced: false
    }
  },
  methods: {
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      console.log(this.ids)
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    getLabel(dictValue, dictType) {
      for (let i = 0; i < this.dict.type[dictType].length; i++) {
        if (this.dict.type[dictType][i].value == dictValue) {
          return this.dict.type[dictType][i].label
        }
      }
    },
    init() {
      console.log('dict.type', this.dict)
      listData({
        dictType: 'shop_ranges'
      }).then(res => {
        this.shopRangesOption = res.rows
      })
      this.getList()
    },
    handleAdd() {
      this.$refs.addVehicle.open()
    },
    handleUpdate(item) {
      getCar(item.id).then(res => {
        if (res.code == 200) {
          this.$refs.addVehicle.open(res.data)
        }
      })
    },
    handleBingGPS(row) {
      const id = row.id;
      this.$router.push({
        path: "/vehicle/bind-gps/vehicle/",
        query: {
          id: row.id,
          name: row.name,
          carnum: row.carnum
        }
      });
    },
    handleMapLocation(row) {
      this.$router.push({
        path: '/vehicle/map',
        query: {
          defaultChoseCar: row.id
        }
      })
    },
    handleCommand(command, row) {
      switch (command) {
        case "handleDelete":
          this.handleDelete(row)
          break
        case "handleBingGPS":
          this.handleBingGPS(row)
          break
        // case "handleMapLocation":
        //   this.handleMapLocation(row)
        default:
          break;
      }
    },
    handleDelete(item) {
      let storesId = item.id || this.ids
      this.$modal.confirm('是否确认删除车辆编号为"' + storesId + '"的数据项？').then(function () {
        return delCar(storesId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/merchant/good/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        isRental: '',
        isCharter: '',
        status: '',        //车辆状态
        sid: '',          //门店id
        cartype: '',         //门店名称
        carnum: '',      //车牌号
        structure: '',    //车身结果
        grarbox: '',        //所在城市
        displacement: '',        //所在区域
        name: '',           // 车辆名称
      }
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      listCar(this.queryParams).then(res => {
        this.storeList = res.rows
        this.total = res.total
        this.loading = false
      })
    }
  },
  mounted() {
    this.init()
  }
}
</script>
<style lang="scss">
.el-table .warning-row {
  background: oldlace !important;
}
</style>
